package com.lzk.org.controller;

import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.org.domain.Department;
import com.lzk.org.query.DepartmentQuery;
import com.lzk.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/department")

@Api(value = "员工的API",description="员工相关的CRUD功能")
public class DepartmentController {

    @Resource
    private IDepartmentService departmentService;

    @GetMapping("/deptTree")
    @ApiOperation(value = "部门树" )
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Department> findAll(){
        return departmentService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryData(departmentQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
